

import HeadPrefab from "../../../component/HeadPrefab";
import { ColorConfig } from "../../../engine/ColorConfig";
import DateUtil from "../../../engine/DateUtil";
import ViewBase from "../../../engine/ViewBase";
import { HttpHelper } from "../../../net/HttpHelper";
import { ClubConfig } from "../../config/ClubConfig";
import { UserModel } from "../../model/UserModel";
import { UIHelper } from "../../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewClubFreezeHistory extends ViewBase {


    private dataList: Array<object> = []
    private defaultItem: cc.Node;
    private txtPage: cc.Label;
    private curPage: number = 1
    private totalPage: number = 1;
    private page_size: number = 2

    onEnable(): void {
        super.onEnable()
        this.curPage = 1
        this.defaultItem = this.findChild(this.node, "scrollView/view/content/defaultItem")
        HttpHelper.get_club_freeze_history(UserModel.instance.nowClub.clubid, (data) => {
            this.dataList = data
            this.refresh();
        })
        let btnPre: cc.Node = this.findChild(this.node, "btnPre")
        let btnNext: cc.Node = this.findChild(this.node, "btnNext")
        this.txtPage = this.getChild(this.node, "txtPage", cc.Label)
        UIHelper.onAddEvent(btnPre, () => {
            if (this.curPage > 1) {
                this.curPage--;
                this.refresh()
            }
        })
        UIHelper.onAddEvent(btnNext, () => {
            if (this.curPage < this.totalPage) {
                this.curPage++
                this.refresh()
            }
        })
        this.refresh()
    }

    private refresh() {
        this.totalPage = this.dataList.length % this.page_size == 0 ? this.dataList.length / this.page_size : Math.floor(this.dataList.length / this.page_size) + 1;
        if (this.totalPage <= 0) {
            this.totalPage = 1;
        }
        this.txtPage.string = this.curPage+"/"+this.totalPage
        this.hideAllChildren(this.defaultItem.parent)
        // for (let i = 0; i < this.dataList.length; i++) {
        for (let i = 0; i < this.page_size; i++) {
            let realIndex: number = (this.curPage - 1) * this.page_size + i
            let data: object = this.dataList[realIndex]
            if(data){
                let itemNode: cc.Node = this.defaultItem.parent.children[i]
                if (!itemNode) {
                    itemNode = cc.instantiate(this.defaultItem)
                    itemNode.parent = this.defaultItem.parent
                }
                itemNode.active = true
                let head: HeadPrefab = this.getChild(itemNode, "head", HeadPrefab)
                head.setHead(data["headimg"], data["lv"])
                let txtVip: cc.Label = this.getChild(itemNode, "txtVip", cc.Label)
                txtVip.string = data["invite"] + ""
                let txtId: cc.Label = this.getChild(itemNode, "txtId", cc.Label)
                txtId.string = "ID:" + data["freezeid"]
                let txtName: cc.Label = this.getChild(itemNode, "txtName", cc.Label)
                txtName.string = "" + data["nick_name"]
                let txtFreezeType: cc.Label = this.getChild(itemNode, "txtFreezeType", cc.Label)
                if (data["freeze_type"] == ClubConfig.Freeze_Type.Freeze) {
                    txtFreezeType.string = "被" + data["handleid"] + "冻结"
                    txtFreezeType.node.color = ColorConfig.hexToColor("#98530A")
                } else {
                    txtFreezeType.string = "被" + data["handleid"] + "解除冻结"
                    txtFreezeType.node.color = ColorConfig.hexToColor("#392aff")
                }
                let txtTime: cc.Label = this.getChild(itemNode, "txtTime", cc.Label)
                txtTime.string = DateUtil.formatDateStr(new Date(data["time"]).getTime())
            }
        }
    }
}
